package com.example.word.ui.activity;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.example.dict.R;
import com.example.word.bean.Dict;
import com.example.word.bean.StudyPlan;
import com.example.word.bean.User;
import com.example.word.bean.Word;
import com.example.word.util.CommonUtils;
import com.example.word.util.HttpUtils;
import com.example.word.util.MessageUtils;
import com.example.word.util.SPUtils;
import com.example.word.widget.ActionBar;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 学习计划
 */
public class AddWordActivity extends AppCompatActivity {
    private Activity mActivity;
    private ActionBar mTitleBar;//标题栏
    private TextView wordEn, ukphone, usphone, wordZh, sentences;
    private Button btnSave;//保存
    private Spinner dictSelect;
    private String account;
    private Word currentWord;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_word);
        mActivity = this;
        wordEn = findViewById(R.id.wordEn);
        ukphone = findViewById(R.id.ukphone);
        usphone = findViewById(R.id.usphone);
        wordZh = findViewById(R.id.wordZh);
        sentences = findViewById(R.id.sentences);
        btnSave = findViewById(R.id.btn_save);
        dictSelect = findViewById(R.id.dictSelect);
        mTitleBar = (ActionBar) findViewById(R.id.myActionBar);
        mTitleBar.setData(mActivity, "新增单词", R.drawable.ic_back,  R.drawable.a1, 0, getResources().getColor(R.color.colorPrimary), new ActionBar.ActionBarClickListener() {
            @Override
            public void onLeftClick() {
                finish();
            }

            @Override
            public void onRightClick() {
            }
        });
        // 设置主题
        if (!com.example.word.ui.activity.MainActivity.colorChange) {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
            btnSave.setBackground(getResources().getDrawable(R.drawable.btn_border_fill));
        } else {
            mTitleBar.setBackgroundColor(getResources().getColor(R.color.myColor));
            btnSave.setBackground(getResources().getDrawable(R.drawable.btn_border_fill_my_color));
        }
        initView();
    }

    private void initView() {
        account = (String) SPUtils.get(mActivity, "account", "");
        HttpUtils.sendGetRequest("/getDictList", new HashMap<>(), mActivity, Dict.class, response -> {
            List<Dict> dictList = response.getDataList();
            List<String> itemsArray = new ArrayList<>();
            for (Dict dict : dictList) {
                itemsArray.add(dict.getDict());
            }
            // 创建 ArrayAdapter 并设置下拉选项的样式
            ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, itemsArray);
            // 设置下拉列表的样式
            adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
            // 将适配器设置到 Spinner
            dictSelect.setAdapter(adapter);
            //Activity之间通过Intent传递值
            currentWord = (Word) getIntent().getSerializableExtra("word");
            if (currentWord != null) {
                sentences.setText(currentWord.getSentences());
                wordZh.setText(currentWord.getWordZh());
                usphone.setText(currentWord.getUsphone());
                ukphone.setText(currentWord.getUkphone());
                wordEn.setText(currentWord.getWord());
                wordEn.setEnabled(false);
                dictSelect.setSelection(itemsArray.indexOf(currentWord.getDict()), true);
            } else {
                wordEn.setEnabled(true);
            }
        });


        //保存
        btnSave.setOnClickListener(v -> {
            Map<String, String> requestParam = new HashMap<>();
            requestParam.put("word", wordEn.getText().toString());
            requestParam.put("wordZh", wordZh.getText().toString());
            requestParam.put("dict", dictSelect.getSelectedItem().toString());
            requestParam.put("ukphone", ukphone.getText().toString());
            requestParam.put("usphone", usphone.getText().toString());
            requestParam.put("sentences", sentences.getText().toString());
            if (currentWord != null) {
                requestParam.put("isUpdate", "Y");
            }
            // 调用 API 客户端的方法
            HttpUtils.sendGetRequest("/saveWord", requestParam, mActivity, Dict.class, response -> {
                MessageUtils.show("保存成功！", mActivity);
                finish();//关闭页面
            });

        });

    }
}
